<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dttx123.gitee.io","root":"/blog/","scheme":"Gemini","version":"8.0.0-rc.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="正则表达式 正则表达式：用于匹配规律规则的表达式，正则表达式最初是科学家对人类神经系统的工作原理的早期研究，现在在编程语言中有广泛的应用，经常用于表单校验，高级搜索等。">
<meta property="og:type" content="article">
<meta property="og:title" content="正则和jq封装">
<meta property="og:url" content="http://dttx123.gitee.io/2017/03/20/javaScript/%E6%AD%A3%E5%88%99%E5%9F%BA%E7%A1%80%E5%92%8CJQuery%E5%B0%81%E8%A3%85/index.html">
<meta property="og:site_name" content="Starry Night">
<meta property="og:description" content="正则表达式 正则表达式：用于匹配规律规则的表达式，正则表达式最初是科学家对人类神经系统的工作原理的早期研究，现在在编程语言中有广泛的应用，经常用于表单校验，高级搜索等。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://dttx123.gitee.io/blog/2017/03/20/javaScript/%E6%AD%A3%E5%88%99%E5%9F%BA%E7%A1%80%E5%92%8CJQuery%E5%B0%81%E8%A3%85/Itcast/lessons/JavaScript%E9%AB%98%E7%BA%A7%E5%A4%87%E8%AF%BE/%E8%AF%BE%E7%A8%8B%E6%A8%A1%E6%9D%BF--AB%E6%A8%A1%E5%BC%8F--6.5/day04/01-%E6%95%99%E5%AD%A6%E8%B5%84%E6%96%99/%E7%AC%94%E8%AE%B0/media/yuan.png">
<meta property="article:published_time" content="2017-03-19T16:00:00.000Z">
<meta property="article:modified_time" content="2020-05-12T16:26:12.735Z">
<meta property="article:author" content="大春哥">
<meta property="article:tag" content="js核心">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://dttx123.gitee.io/blog/2017/03/20/javaScript/%E6%AD%A3%E5%88%99%E5%9F%BA%E7%A1%80%E5%92%8CJQuery%E5%B0%81%E8%A3%85/Itcast/lessons/JavaScript%E9%AB%98%E7%BA%A7%E5%A4%87%E8%AF%BE/%E8%AF%BE%E7%A8%8B%E6%A8%A1%E6%9D%BF--AB%E6%A8%A1%E5%BC%8F--6.5/day04/01-%E6%95%99%E5%AD%A6%E8%B5%84%E6%96%99/%E7%AC%94%E8%AE%B0/media/yuan.png">

<link rel="canonical" href="http://dttx123.gitee.io/2017/03/20/javaScript/%E6%AD%A3%E5%88%99%E5%9F%BA%E7%A1%80%E5%92%8CJQuery%E5%B0%81%E8%A3%85/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>正则和jq封装 | Starry Night</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line toggle-line-first"></span>
        <span class="toggle-line toggle-line-middle"></span>
        <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Starry Night</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">这就朕为你打下的江山</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://dttx123.gitee.io/2017/03/20/javaScript/%E6%AD%A3%E5%88%99%E5%9F%BA%E7%A1%80%E5%92%8CJQuery%E5%B0%81%E8%A3%85/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
      <meta itemprop="name" content="大春哥">
      <meta itemprop="description" content="IT">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Starry Night">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          正则和jq封装
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2017-03-20 00:00:00" itemprop="dateCreated datePublished" datetime="2017-03-20T00:00:00+08:00">2017-03-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-13 00:26:12" itemprop="dateModified" datetime="2020-05-13T00:26:12+08:00">2020-05-13</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/js%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">js基础</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h1><blockquote>
<p>正则表达式：用于匹配规律规则的表达式，正则表达式最初是科学家对人类神经系统的工作原理的早期研究，现在在编程语言中有广泛的应用，经常用于表单校验，高级搜索等。</p>
</blockquote>
<a id="more"></a>

<h2 id="创建正则表达式"><a href="#创建正则表达式" class="headerlink" title="创建正则表达式"></a>创建正则表达式</h2><p>【07-正则表达式的创建.html】</p>
<p>构造函数的方式</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> regExp = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="regexp">/\d/</span>);</span><br></pre></td></tr></table></figure>

<p>正则字面量</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> regExp = <span class="regexp">/\d/</span>;</span><br></pre></td></tr></table></figure>

<p>正则的使用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/\d/.test(<span class="string">"aaa1"</span>);</span><br></pre></td></tr></table></figure>

<h2 id="元字符"><a href="#元字符" class="headerlink" title="元字符"></a>元字符</h2><blockquote>
<p>正则表达式由一些普通字符和元字符组成，普通字符包括大小写字母、数字等，而元字符则具有特殊的含义。</p>
</blockquote>
<h3 id="常见元字符"><a href="#常见元字符" class="headerlink" title="常见元字符"></a>常见元字符</h3><p><img src="/blog/2017/03/20/javaScript/%E6%AD%A3%E5%88%99%E5%9F%BA%E7%A1%80%E5%92%8CJQuery%E5%B0%81%E8%A3%85/Itcast/lessons/JavaScript%E9%AB%98%E7%BA%A7%E5%A4%87%E8%AF%BE/%E8%AF%BE%E7%A8%8B%E6%A8%A1%E6%9D%BF--AB%E6%A8%A1%E5%BC%8F--6.5/day04/01-%E6%95%99%E5%AD%A6%E8%B5%84%E6%96%99/%E7%AC%94%E8%AE%B0/media/yuan.png" alt></p>
<p><code>|</code>表示或，优先级最低</p>
<p><code>()</code>优先级最高，表示分组</p>
<h3 id="字符类的元字符"><a href="#字符类的元字符" class="headerlink" title="字符类的元字符"></a>字符类的元字符</h3><p><code>[]</code>在正则表达式中表示一个字符的位置，[]里面写这个位置可以出现的字符。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="regexp">/[abc]/</span>);<span class="comment">//匹配a,b,c</span></span><br></pre></td></tr></table></figure>

<p><code>[^]</code>在中扩号中的^表示非的意思。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//^表示该位置不可以出现的字符</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="regexp">/[^abc]/</span>);<span class="comment">//匹配除了a，b，c以外的其他字符</span></span><br></pre></td></tr></table></figure>

<p><code>[a-z]</code> <code>[1-9]</code>表示范围</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="regexp">/[a-z]/</span>.test(<span class="string">"d"</span>));<span class="comment">//小写字母</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="regexp">/[A-Z]/</span>.test(<span class="string">"d"</span>));<span class="comment">//大写字母</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="regexp">/[0-9]/</span>.test(<span class="string">"8"</span>));<span class="comment">//数字</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="regexp">/[a-zA-Z0-9]/</span>);<span class="comment">//所有的小写字母和大写字母以及数字</span></span><br></pre></td></tr></table></figure>

<h3 id="边界类元字符"><a href="#边界类元字符" class="headerlink" title="边界类元字符"></a>边界类元字符</h3><blockquote>
<p>我们前面学习的正则只要有满足的条件的就会返回true，并不能做到精确的匹配。</p>
</blockquote>
<p>【12-正则边界.html】</p>
<p>^表示开头   <strong><em>[]里面的^表示取反</em></strong></p>
<p>$表示结尾</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="regexp">/^chuan/</span>.test(<span class="string">"dachuan"</span>));<span class="comment">//必须以chuan开头</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="regexp">/chuan$/</span>.test(<span class="string">"chuang"</span>));<span class="comment">//必须以chuan结尾</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="regexp">/^chuan$/</span>.test(<span class="string">"chuan"</span>));<span class="comment">//精确匹配chuan</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//精确匹配chuan,表示必须是这个</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="regexp">/^chuan$/</span>.test(<span class="string">"chuanchuan"</span>));<span class="comment">//fasle</span></span><br></pre></td></tr></table></figure>

<h3 id="量词类元字符"><a href="#量词类元字符" class="headerlink" title="量词类元字符"></a>量词类元字符</h3><blockquote>
<p>量词用来控制出现的次数，一般来说量词和边界会一起使用</p>
</blockquote>
<p>【13-正则量词.html】</p>
<ol>
<li><code>*</code>表示能够出现0次或者更多次，x&gt;=0;</li>
<li><code>+</code>表示能够出现1次或者多次，x&gt;=1</li>
<li><code>?</code>表示能够出现0次或者1次，x=0或者x=1</li>
<li><code>{n}</code>表示能够出现n次</li>
<li><code>{n,}</code>表示能够出现n次或者n次以上</li>
<li><code>{n,m}</code>表示能够出现n-m次</li>
</ol>
<p>思考：如何使用{}来表示*+? </p>
<h2 id="正则的使用"><a href="#正则的使用" class="headerlink" title="正则的使用"></a>正则的使用</h2><h3 id="正则测试"><a href="#正则测试" class="headerlink" title="正则测试"></a>正则测试</h3><ol start="2">
<li><p>验证QQ</p>
<ul>
<li>只能是数字</li>
<li>开头不能是0</li>
<li>长度为5-11位</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> qqReg = <span class="regexp">/^[1-9]\d&#123;4,10&#125;$/</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>验证手机</p>
<ul>
<li>11位数字组成</li>
<li>号段13[0-9] 147 15[0-9] 17[0178] 18[0-9]</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> mobileReg = <span class="regexp">/^(13[0-9]|147|15[0-9]|17[0178]|18[0-9])\d&#123;8&#125;$/</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>验证邮箱</p>
<ul>
<li>前面是字母或者数字</li>
<li>必须有@</li>
<li>@后面是字母或者数字</li>
<li>必须有.</li>
<li>.后面是字母或者数字</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> emailReg = <span class="regexp">/^\w+@\w+(\.\w+)+$/</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>验证姓名</p>
<ul>
<li>只能是汉字</li>
<li>长度2-6位之间</li>
<li>汉字范围[\u4e00-\u9fa5]</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> nameReg = <span class="regexp">/^[\u4e00-\u9fa5]&#123;2,6&#125;$/</span>;</span><br></pre></td></tr></table></figure>



</li>
</ol>
<h3 id="正则替换"><a href="#正则替换" class="headerlink" title="正则替换"></a>正则替换</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">"   123AD  asadf   asadfasf  adf  "</span>;</span><br><span class="line"><span class="number">1</span>  替换掉字符串中的所有空白</span><br><span class="line"><span class="number">2.</span> 将所有的ad/AD替换成xx</span><br></pre></td></tr></table></figure>



<h1 id="jQuery框架封装"><a href="#jQuery框架封装" class="headerlink" title="jQuery框架封装"></a>jQuery框架封装</h1><h2 id="jq的基本架构"><a href="#jq的基本架构" class="headerlink" title="jq的基本架构"></a>jq的基本架构</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">window</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> jQuery = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 把jQuery函数暴露到全局中</span></span><br><span class="line">    <span class="built_in">window</span>.jQuery = <span class="built_in">window</span>.$ = jQuery;</span><br><span class="line">&#125;)(<span class="built_in">window</span>);</span><br></pre></td></tr></table></figure>

<h2 id="jq的选择器"><a href="#jq的选择器" class="headerlink" title="jq的选择器"></a>jq的选择器</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">window</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> jQuery = <span class="function"><span class="keyword">function</span>(<span class="params">selector</span>)</span>&#123;</span><br><span class="line">        <span class="comment">// 根据选择器来获取元素</span></span><br><span class="line">        <span class="keyword">var</span> ele = <span class="built_in">document</span>.querySelectorAll(selector);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 把获取到的元素遍历添加到jq实例对象上</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; ele.length; i++)&#123;</span><br><span class="line">            <span class="keyword">this</span>[i] = ele[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 手动维护length属性为获取元素的个数</span></span><br><span class="line">        <span class="keyword">this</span>.length = ele.length;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">window</span>.jQuery = <span class="built_in">window</span>.$ = jQuery;</span><br><span class="line">&#125;)(<span class="built_in">window</span>);</span><br></pre></td></tr></table></figure>

<h2 id="原型添加方法"><a href="#原型添加方法" class="headerlink" title="原型添加方法"></a>原型添加方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">window</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> jQuery = <span class="function"><span class="keyword">function</span>(<span class="params">selector</span>)</span>&#123;</span><br><span class="line">        <span class="comment">// ...</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 原型替换，给jq原型中添加成员</span></span><br><span class="line">    jQuery.prototype = &#123;</span><br><span class="line">        <span class="keyword">constructor</span>: jQuery,</span><br><span class="line">        css: function()&#123;</span><br><span class="line">            <span class="comment">// css 操作样式功能...</span></span><br><span class="line">        &#125;,</span><br><span class="line">        html: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="comment">// html 操作文本内容功能...</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//...</span></span><br><span class="line">    &#125;;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">window</span>.jQuery = <span class="built_in">window</span>.$ = jQuery;</span><br><span class="line">&#125;)(<span class="built_in">window</span>);</span><br></pre></td></tr></table></figure>

<h2 id="省去new的操作"><a href="#省去new的操作" class="headerlink" title="省去new的操作"></a>省去new的操作</h2><p>使用工厂函数来省去new可以得到实例对象，方便得到实例对象不用自己手动new</p>
<h2 id="改变构造函数init的原型"><a href="#改变构造函数init的原型" class="headerlink" title="改变构造函数init的原型"></a>改变构造函数init的原型</h2><p>得到的实例对象访问不到jq原型中的成员，改变init构造函数的原型为jQuery的原型，使其init的实例对象可以访问的到jq原型中的成员。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jQuery.fn.init.prototype = jQuery.fn;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/js%E6%A0%B8%E5%BF%83/" rel="tag"># js核心</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2017/02/07/javaScript/%E5%AF%B9%E8%B1%A1%E5%92%8C%E5%8E%9F%E5%9E%8B/" rel="prev" title="js对象和原型">
      <i class="fa fa-chevron-left"></i> js对象和原型
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2017/04/03/javaScript/%E5%87%BD%E6%95%B0/" rel="next" title="函数基础">
      函数基础 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#正则表达式"><span class="nav-text">正则表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#创建正则表达式"><span class="nav-text">创建正则表达式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#元字符"><span class="nav-text">元字符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#常见元字符"><span class="nav-text">常见元字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字符类的元字符"><span class="nav-text">字符类的元字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#边界类元字符"><span class="nav-text">边界类元字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#量词类元字符"><span class="nav-text">量词类元字符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#正则的使用"><span class="nav-text">正则的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#正则测试"><span class="nav-text">正则测试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#正则替换"><span class="nav-text">正则替换</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#jQuery框架封装"><span class="nav-text">jQuery框架封装</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#jq的基本架构"><span class="nav-text">jq的基本架构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#jq的选择器"><span class="nav-text">jq的选择器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#原型添加方法"><span class="nav-text">原型添加方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#省去new的操作"><span class="nav-text">省去new的操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#改变构造函数init的原型"><span class="nav-text">改变构造函数init的原型</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="大春哥"
      src="/blog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">大春哥</p>
  <div class="site-description" itemprop="description">IT</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">35</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">大春哥</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

</body>
</html>
